package org.oschina.api.user.follow.controller;

import org.oschina.api.BaseController;
import org.oschina.api.user.follow.service.UserFollowService;
import org.oschina.api.user.follow.vo.UserFollowVo;
import org.oschina.bean.Pager;
import org.oschina.bean.Result;
import org.oschina.config.Config;
import org.oschina.handler.validator.annotation.Validate;
import org.oschina.handler.validator.annotation.ValidateField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value=Config.API_PATH+"/user/follow")
public class UserFollowController extends BaseController {

	@Autowired
	private UserFollowService followService;
	
	/**
	 * 根据目标返回
	 * @param pn
	 * @return
	 */
	@RequestMapping(value="/fans",method=RequestMethod.GET)
	public Pager<UserFollowVo> findVoByTarget(Integer pn)
	{
		if(pn==null || pn<=0)
			pn=1;
		
		return followService.findVoByTarget(pn);
	}
	
	/**
	 * 根据源返回
	 * @param pn
	 * @return
	 */
	@RequestMapping(value="/target",method=RequestMethod.GET)
	public Pager<UserFollowVo> findVoBySource(Integer pn)
	{
		if(pn==null || pn<=0)
			pn=1;
		
		return followService.findVoBySource(pn);
	}
	
	/**
	 * 创建关注
	 * @param target
	 * @return
	 */
	@Validate
	@RequestMapping(value="/create",method=RequestMethod.POST)
	public Result create
	(
		@Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer target
	)
	{
		return followService.create(target);
	}
	
	/**
	 * 取消关注
	 * @param target
	 * @return
	 */
	@Validate
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public Result delete
	(
		@Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer target
	)
	{
		return followService.delete(target);
	}
}